﻿@inherits AdminPage

<Layout Style="min-height:100vh;">
    @if (IsLoaded)
    {
        <Sider Collapsible Theme="ThemeSider">
            <div class="logo" />
            <Menu Theme="ThemeMenu" Mode="MenuMode.Inline" Accordion="Context.UserSetting.Accordion">
                @if (UserMenus != null)
                {
                    foreach (var item in UserMenus)
                    {
                        if (item.Children != null && item.Children.Count > 0)
                        {
                            RenderFragment subTitle = @<span>
                                <Icon Type="@item.Icon" Theme="outline" />
                                <span>@item.Name</span>
                            </span>;
                            <SubMenu Key="@item.Id" TitleTemplate=subTitle>
                                @foreach (var child in item.Children)
                                {
                                    <MenuItem Key="@child.Id" OnClick="e=>Context.Navigate(child)">
                                        <Icon Type="@child.Icon" Theme="outline" />
                                        <span>@child.Name</span>
                                    </MenuItem>
                                }
                            </SubMenu>
                        }
                        else
                        {
                            <MenuItem Key="@item.Id" OnClick="e=>Context.Navigate(item)">
                                <Icon Type="@item.Icon" Theme="outline" />
                                <span>@item.Name</span>
                            </MenuItem>
                        }
                    }
                }
            </Menu>
        </Sider>
        <Layout Class="site-layout">
            <Header Class="site-layout-background" Style="padding:0;">
                <div style="padding:0 10px;display:flex;justify-content:space-between;">
                    <h1>@CurrentUser?.AppName</h1>
                    <NavTopRight OnMenuClick="OnMenuClick" />
                </div>
            </Header>
            <Content>
                @if (Context.UserSetting.MultiTab)
                {
                    <PageTabs Menu="CurrentMenu" Items="TabMenus" />
                }
                else
                {
                    <PageSingle Menu="CurrentMenu" OnHome="OnHome" />
                }
            </Content>
            @if (KaConfig.Option.Footer != null)
            {
                <Footer Style="text-align:center">@KaConfig.Option.Footer</Footer>
            }
        </Layout>
    }
</Layout>

@code{
    private SiderTheme ThemeSider => Context.UserSetting.IsLight ? SiderTheme.Light : SiderTheme.Dark;
    private MenuTheme ThemeMenu => Context.UserSetting.IsLight ? MenuTheme.Light : MenuTheme.Dark;

    private void OnHome() => OnMenuClick("home");

    private void OnMenuClick(string id)
    {
        switch (id)
        {
            case "home":
                Context.NavigateToHome();
                break;
            case "profile":
                Context.Navigate(new("个人中心", "user", typeof(UserProfile), "当前用户个人中心和安全设置。"));
                break;
            case "logout":
                UI.Confirm("确定要退出系统？", async () =>
                {
                    var result = await SignOutAsync();
                    if (result.IsValid)
                    {
                        OnLogout?.Invoke();
                    }
                });
                break;
        }
    }
}